package gui;


import interfaces.BankRegistry;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import money.Money;

import banking.*;

public class GUItransferMoney extends JFrame {
	
	JTextField textfield_reason;
	JTextField textfield_accountNumber;
	JTextField textfield_value;
	JTextField textfield_acceptorNumber;
	
	JButton	button_transferMoney;
	
	JLabel label_accountNumber;
	JLabel label_reason;
	JLabel label_value;
	JLabel label_acceptorNumber;
	
	public GUItransferMoney() {

		this.getContentPane().setLayout(null);

		this.transferMoneyWindow();

		this.addWindowListener(new WindowListener() {

			public void windowClosed(WindowEvent arg0) {
			}

			public void windowActivated(WindowEvent e) {
			}

			public void windowClosing(WindowEvent e) {
				
			}
			
			public void windowDeactivated(WindowEvent e) {
			}

			public void windowDeiconified(WindowEvent e) {
			}

			public void windowIconified(WindowEvent e) {
			}

			public void windowOpened(WindowEvent e) {
			}
		});

	}
	
	public void transferMoneyWindow() 
	{

		// Instanzieren:
		textfield_accountNumber = new JTextField();
		textfield_reason = new JTextField();
		textfield_value = new JTextField();
		textfield_acceptorNumber = new JTextField();
		
		button_transferMoney = new JButton("transfer Money");
		
		label_accountNumber = new JLabel("Account Number:");
		label_reason = new JLabel("Reason:");
		label_value = new JLabel("Value:");
		label_acceptorNumber = new JLabel("acceptor Number");
		
		
		//Action Listener
		button_transferMoney.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					transferMoney();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}

		});
		
		// Positionen festlegen
		//textfield_dm.setBounds(5,10,400,25);
		textfield_accountNumber.setBounds(150,40,200,25);
		textfield_reason.setBounds(150, 80, 200, 25);
		textfield_value.setBounds(150, 120, 200, 25);
		textfield_acceptorNumber.setBounds(150, 160, 200, 25);
		
		button_transferMoney.setBounds(75, 240, 200, 30);
		
		label_accountNumber.setBounds(10, 40, 150, 25);
		label_reason.setBounds(10, 80, 150, 25);
		label_value.setBounds(10, 120, 150, 25);
		label_acceptorNumber.setBounds(10, 160, 150, 25);
		
		
		
		// add window
		this.getContentPane().add(textfield_accountNumber);
		this.getContentPane().add(textfield_reason);
		this.getContentPane().add(textfield_value);
		this.getContentPane().add(textfield_acceptorNumber);
		
		this.getContentPane().add(button_transferMoney);
		
		this.getContentPane().add(label_accountNumber);
		this.getContentPane().add(label_reason);
		this.getContentPane().add(label_value);
		this.getContentPane().add(label_acceptorNumber);

		this.pack();
	}

	public void transferMoney() throws Exception{
		Money money = new Money(0);
		
		int	accountNumber = Integer.parseInt(textfield_accountNumber.getText());
		String reason = textfield_reason.getText();
		money.setValue(Long.parseLong(textfield_value.getText()));
		int acceptorNumber = Integer.parseInt(textfield_acceptorNumber.getText());
		BankImpl Sparkasse = BankRegistry.loockup("Sparkasse");
		Sparkasse.transferMoney(acceptorNumber, money, reason, accountNumber);
		Sparkasse.accountStatement(1002, "text/plain");
	}
}